﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Atomic.Permission.Entities;
using Atomic.Common;
using Newtonsoft.Json;
using Atomic.Permission;

namespace Atomic.Web.Handlers.Sys
{
    /// <summary>
    /// RoleHandler 的摘要说明
    /// </summary>
    public class RoleHandler : BaseHandler
    {
        static Dictionary<string, Action<HttpContext>> actions = new Dictionary<string, Action<HttpContext>>();
        protected override Dictionary<string, Action<HttpContext>> Actions
        {
            get { return actions; }
        }

        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="context"></param>
        public void GetRoleList(HttpContext context)
        {
            IRole role = ContextFactory.Context.GetObject("Role") as IRole;
            Pager<RoleEntity> pagerEntity = role.GetPager(new Pager()
            {
                Index = 1,
                Rows = int.MaxValue
            });
            context.Response.Write(JsonConvert.SerializeObject(new
            {
                total = pagerEntity.Total,
                rows = pagerEntity.Records
            }));
        }

        /// <summary>
        /// 获取指定ID角色基本信息
        /// </summary>
        /// <param name="context"></param>
        public void GetRole(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IRole role = ContextFactory.Context.GetObject("Role") as IRole;
            RoleEntity entity = role.Get(id);
            context.Response.Write(JsonConvert.SerializeObject(entity));
        }

        /// <summary>
        /// 新建角色，如果角色ID已经存在则，更新角色信息
        /// </summary>
        /// <param name="context"></param>
        public void AddRole(HttpContext context)
        {
            IRole role = ContextFactory.Context.GetObject("Role") as IRole;
            RoleEntity entity = new RoleEntity()
            {
                ID = context.Request["ID"].ToInt32(0),
                Name = context.Request["Name"].Trim(),
                Description = context.Request["Description"].Trim(),
                Sort = 1,
                Enabled = context.Request["Enabled"] == "1" ? true : false
            };
            if ((entity.ID > 0) ? role.Update(entity, entity.ID) : role.Add(entity))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = role.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 删除指定ID角色
        /// </summary>
        /// <param name="context"></param>
        public void RemoveRole(HttpContext context)
        {
            int id = context.Request["ID"].ToInt32(0);
            IRole role = ContextFactory.Context.GetObject("Role") as IRole;
            if (role.Remove(id))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = role.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 获取角色所有功能权限列表
        /// </summary>
        /// <param name="context"></param>
        public void GetResourceFunList(HttpContext context)
        {
            int roleID = context.Request["RoleID"].ToInt32(0);
            IRoleFormFun roleFun = ContextFactory.Context.GetObject("RoleFormFun") as IRoleFormFun;
            var list = roleFun.GetList(m => m.RoleID == roleID);
            context.Response.Write(JsonConvert.SerializeObject(list));
        }

        /// <summary>
        /// 添加角色资源权限
        /// </summary>
        /// <param name="context"></param>
        public void AddResourceFun(HttpContext context)
        {
            int roleID = context.Request["RoleID"].ToInt32(0);
            int formID = context.Request["FormID"].ToInt32();
            int funID = context.Request["FunID"].ToInt32();
            IRoleFormFun roleFun = ContextFactory.Context.GetObject("RoleFormFun") as IRoleFormFun;
            bool b = roleFun.Add(new RoleFormFunEntity()
                {
                    FormID = formID,
                    FunID = funID,
                    RoleID = roleID
                });
            if (b)
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = roleFun.GetError().Message
                }));
            }
        }

        /// <summary>
        /// 删除角色资源权限
        /// </summary>
        /// <param name="context"></param>
        public void RemoveResourceFun(HttpContext context)
        {
            int roleID = context.Request["RoleID"].ToInt32(0);
            int formID = context.Request["FormID"].ToInt32();
            int funID = context.Request["FunID"].ToInt32();
            IRoleFormFun roleFun = ContextFactory.Context.GetObject("RoleFormFun") as IRoleFormFun;
            var entity = roleFun.GetList(m => m.RoleID == roleID && m.FormID == formID && m.FunID == funID).FirstOrDefault();
            if (entity == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = "角色不存在次功能点"
                }));
                return;
            }
            if (roleFun.Remove(entity.ID))
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = true
                }));
            }
            else
            {
                context.Response.Write(JsonConvert.SerializeObject(new
                {
                    Success = false,
                    Message = roleFun.GetError().Message
                }));
            }
        }
    }
}